package br.com.sovi.gwt.client.historico;

import br.com.sovi.gwt.client.application.Application;
import br.com.sovi.gwt.client.application.ApplicationConstants;
import br.com.sovi.gwt.client.application.SingleAttributeListener;
import br.com.sovi.gwt.client.component.AsyncReturn;
import br.com.sovi.gwt.client.historico.marcador.MarcadorInfoController;

import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.layout.VerticalLayout;

public class InformacaoMarcadorPanel extends Panel {

	private Label dataServidorLabel, geoCodeLabel;

	private MapWidget map;

	public InformacaoMarcadorPanel(MapWidget map) {
		super("Informações do Marcador");
		this.map = map;

		this.setLayout(new VerticalLayout(3));
		this.setStyleName("informacaoMarcadorPanel");

		this.buildComponents();

		Application.get().addAttributeListener(
				new SingleAttributeListener(ApplicationConstants.HISTORICO_MARCADOR_SELECIONADO) {
					@Override
					public void added(Object value) {
						populate();
					}

					@Override
					public void removed() {
						clearComponents();
					}
				});
	}

	private void buildComponents() {
		dataServidorLabel = new Label();
		dataServidorLabel.setStyleName("dataServidorLabel");

		geoCodeLabel = new Label();
		geoCodeLabel.setStyleName("geoCodeLabel");

		this.add(buildNavigationButtons());
		this.add(dataServidorLabel);
		this.add(geoCodeLabel);
	}

	public void populate() {
		clearComponents();

		buildServerTime();
		buildGeoCode();
		populateNavigationButtons();
	}

	private void clearComponents() {
		dataServidorLabel.setText("");
		geoCodeLabel.setText("");
	}

	private Button previousButton;

	private Button nextButton;

	private ChangePontoClickHandler previousMarkerHandler, nextMarkerHandler;

	private void populateNavigationButtons() {
		HistoricoMarker previous = MarcadorInfoController.getPreviousMarker();
		previousMarkerHandler.setMarker(previous);
		previousButton.setEnabled(previous != null);

		HistoricoMarker next = MarcadorInfoController.getNextMarker();
		nextMarkerHandler.setMarker(next);
		nextButton.setEnabled(next != null);
	}

	private HorizontalPanel buildNavigationButtons() {
		HorizontalPanel pontosPanel = new HorizontalPanel();

		previousButton = new Button("Anterior");
		previousMarkerHandler = new ChangePontoClickHandler(map);
		previousButton.addClickHandler(previousMarkerHandler);
		pontosPanel.add(previousButton);

		nextButton = new Button("Próximo");
		nextMarkerHandler = new ChangePontoClickHandler(map);
		nextButton.addClickHandler(nextMarkerHandler);
		pontosPanel.add(nextButton);

		return pontosPanel;
	}

	private void buildServerTime() {
		String data = MarcadorInfoController.getDataServidor();
		dataServidorLabel.setText(data);
	}

	protected void buildGeoCode() {
		MarcadorInfoController.getAddress(new AsyncReturn<String>() {
			@Override
			public void onReturn(String t) {
				geoCodeLabel.setText(t);
			}
		});
	}

}
